package com.jinlejia.erp.ui.common;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityChangePasswordBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.utils.MD5Utils;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 修改密码（Activity）
 */

public class ChangePwdActivity extends AppCompatActivity {

    /**
     * 正则表达式:验证密码(不包含特殊字符)
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
    private ProgressDialog progressDialog;

    private ActivityChangePasswordBinding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_change_password);
        binding.backBtn.setOnClickListener(this::onClick);
        binding.commit.setOnClickListener(this::onClick);
        initEvent();
    }

    /**
     * 初始化事件
     */
    private void initEvent() {
        progressDialog = new ProgressDialog(this,"请稍等...");
        //原密码
        binding.oldPasswordEdit.setOnFocusChangeListener((view, b) -> {
            if (b){
                binding.oldPasswordEdit.setTextColor(getResources().getColor(R.color.yellow));
                binding.oldPassword.setTextColor(getResources().getColor(R.color.yellow));
                binding.oldDownLine.setBackgroundResource(R.color.yellow);
            }else {
                binding.oldPasswordEdit.setTextColor(getResources().getColor(R.color.hintColor));
                binding.oldPassword.setTextColor(getResources().getColor(R.color.textColor));
                binding.oldDownLine.setBackgroundResource(R.color.hintColor);
            }
        });
        //新密码
        binding.newPasswordEdit.setOnFocusChangeListener((view, b) -> {
            if (b){
                binding.newPasswordEdit.setTextColor(getResources().getColor(R.color.yellow));
                binding.newPassword.setTextColor(getResources().getColor(R.color.yellow));
                binding.newDownLine.setBackgroundResource(R.color.yellow);
            }else {
                binding.newPasswordEdit.setTextColor(getResources().getColor(R.color.hintColor));
                binding.newPassword.setTextColor(getResources().getColor(R.color.textColor));
                binding.newDownLine.setBackgroundResource(R.color.hintColor);
            }
        });
        //确认密码
        binding.confirmPasswordEdit.setOnFocusChangeListener((view, b) -> {
            if (b){
                binding.confirmPasswordEdit.setTextColor(getResources().getColor(R.color.yellow));
                binding.confirmPassword.setTextColor(getResources().getColor(R.color.yellow));
                binding.confirmDownLine.setBackgroundResource(R.color.yellow);
            }else {
                binding.confirmPasswordEdit.setTextColor(getResources().getColor(R.color.hintColor));
                binding.confirmPassword.setTextColor(getResources().getColor(R.color.textColor));
                binding.confirmDownLine.setBackgroundResource(R.color.hintColor);
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.commit:
                if (TextUtils.isEmpty(binding.oldPasswordEdit.getText().toString())){
                    CustomToast.showToast(this,"原密码不能为空",2000);
                }else if (TextUtils.isEmpty(binding.newPasswordEdit.getText().toString())){
                    CustomToast.showToast(this,"新密码不能为空",2000);
                }else if (binding.newPasswordEdit.getText().toString().equals(binding.oldPasswordEdit.getText().toString())){
                    CustomToast.showToast(this,"原密码与新密码不可一致",2000);
                }else if (!binding.newPasswordEdit.getText().toString().equals(binding.confirmPasswordEdit.getText().toString())){
                    CustomToast.showToast(this,"两次输入的密码不一致",2000);
                }else if (!isPassword(binding.newPasswordEdit.getText().toString())){
                    CustomToast.showToast(this,"密码必须包含大写小写字母与数字并且在8~20位之间",2000);
                }
                else {
                    Commit();
                }
                break;
        }
    }

    /**
     * 提交
     */
    public void Commit(){
        progressDialog.show();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("oldPassword", MD5Utils.md5(binding.oldPasswordEdit.getText().toString()));
        jsonObject.put("newPassword", MD5Utils.md5(binding.newPasswordEdit.getText().toString()));

        OkHttpUtils
                .postString()
                .url(ApiConfig.UPDATE_PWD)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        if (DataFilter.filter(ChangePwdActivity.this, response,null)) {
                            CustomToast.showToast(ChangePwdActivity.this,"密码修改成功",2000);
                            finish();
                        }

                    }
                });
    }

    public boolean isPassword(String password){
        String regex="^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,20}$";
        Pattern p=Pattern.compile(regex);
        Matcher m=p.matcher(password);
        boolean isMatch=m.matches();
        return isMatch;
    }

    @Override
    protected void onDestroy() {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        super.onDestroy();
    }
}
